class Tag < ActiveRecord::Base
  has_many :interests
  has_many :tagEvents
  
  def Tag.popular
  tagsNumberLimit = 30
  query = "SELECT * FROM (SELECT * FROM tags T ORDER BY counter DESC LIMIT #{tagsNumberLimit.to_s}) as B ORDER BY B.name"
#   	@tags=Tag.find(:all, :order => "counter", :limit => 30)
  	@tags = Tag.find_by_sql(query)
  end
  
  # returns a coefficient between 1 and 2 based on 
  # the number of occurences of this tag in the tag events
  def usage_frequency
  	counter = self.counter
  	total = Tag.count_by_sql("SELECT COUNT(*) FROM tags")
  	usage =((total+counter*5).to_f/(total))
  	return usage
  end
  
  
  def Tag.createTagWithName(tag_name)
			tag = Tag.new
			tag.name = tag_name
			tag.counter = 0
			tag.save
			return tag
		end
  
end
